import {
  ClassicModel
} from '../../models/classic.js'
import {
  LikeModel
} from '../../models/like.js'
let Classic = new ClassicModel()
let likeModel = new LikeModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 不管怎么样, 给一个初始值
    classic: null,
    latest: true,
    first: false,
    likeCount: 0,
    likeStatus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //数据更新, Storage
    let latest = Classic.getLatest((res) => {
      // 将获取过来的数据添加data中
      console.log(res)
      this.setData({
        classic: res,
        likeCount: res.fav_nums,
        likeStatus: res.like_status
      })
      console.log(this.data)
    })
  },

  //喜欢和取消喜欢
  onLike: function (event) {
    let behavior = event.detail.behavior
    likeModel.like(behavior, this.data.classic.id, this.data.classic.type)
  },
  
  //上一个条目
  onNext: function (event) {
      this._updateClassic('next')
  },
  

  //下一个条目
  onPrevious: function () {
    this._updateClassic('previous')
  },

  //更新数据
  _updateClassic: function(nextOrPrevious) {
    let index = this.data.classic.index;
    console.log(index)
    Classic.getClassic(index, nextOrPrevious, (res) => {
      //获取到数据之后更新数据
      this._getLikeStatus(res.id, res.type)
      this.setData({
        classic: res,
        latest: Classic.isLatest(res.index),
        first: Classic.isFirst(res.index)
      })
    })
  },

  //获取到喜欢状态
  _getLikeStatus: function(artID, category) {
       likeModel.getClassicLikeStatus(artID, category,(res) => {
           this.setData({
               likeCount: res.fav_nums,
               likeStatus: res.like_status

           })
       })
  }
})